package com.eigaland.change_machine.business.show

import com.eigaland.change_machine.BuildConfig
import com.eigaland.change_machine.business.network.RetrofitClient
import com.eigaland.change_machine.displaymodel.ShowListDisplayModel
import com.eigland.eigaland_lib.base.BaseRepository
import com.eigland.eigaland_lib.network.MovieService
import movie6.posprintercept2.extensions.formatServerDate
import movie6.posprintercept2.extensions.getNdayAfter
import movie6.posprintercept2.extensions.splitList
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

public class ShowListFragmentRepository : BaseRepository() {
    suspend fun getShowList(movieId:String,cinemaId:String,date:String):ShowListDisplayModel{
       var result =  RetrofitClient.getService(MovieService::class.java,BuildConfig.domain).showListByCinemaMovieDate(cinemaId,movieId,date).await()

        var displayModel = ShowListDisplayModel()

        var threshold = 5

        var list = ArrayList<ShowListItemVM>()

        result.success?.let { outside->
            outside.content.let {
                for(index in 0 until it.size){
                    var item = ShowListItemVM()
                    item.startTime = it[index].show.startTime.formatServerDate("HH:mm")
                    item.endTime = it[index].show.endTime.formatServerDate("HH:mm")
                    item.showHouse = it[index].house.name
                    item.showId = it[index].show.uuid
                    item.discountType = "超级优惠"
                    item.showPrice = "123"
                    item.showType = it[index].movieVersion.name
                    list.add(item)
                }
            }
        }



        displayModel.showList.addAll(list.splitList(threshold))

        return displayModel

    }

    fun getDateList():ShowListDisplayModel{
        var displayModel = ShowListDisplayModel()
        var dateList = ArrayList<ShowListDateVM>()
        for(index in  0 until 6){
            var vm = ShowListDateVM()
            vm.isCheckCurrent = index == 0
            vm.date = SimpleDateFormat("MM/dd").format(Calendar.getInstance().getNdayAfter(index))
            vm.apiDate = SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getNdayAfter(index))

            dateList.add(vm)

        }
        displayModel.dateList.addAll(dateList)
        return displayModel

    }
}